package com.jiaanpei.frameproject.wxapi;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiaanpei.frameproject.R;
import com.jiaanpei.frameproject.app.Constants;
import com.jiaanpei.frameproject.base.BaseActivity;
import com.jiaanpei.frameproject.utils.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;
    @BindView(R.id.tv_pay_text)
    TextView tv_pay_text;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.btn_confirm)
    Button btn_confirm;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private final static String ERR_OK = "支付成功";
    private final static String ERR_USER_CANCEL = "取消支付";
    private final static String ERR_COMM = "支付异常";
    private final static String ERR_DEFAULT = "未知错误";
    private String payMsg = "";
    public static String orderNo = "";
    public static String Type = "";
    public static String BBYP_TYPE = "bbyp_type";
    public static String JIAANPEI_TYPE = "jiaanpe_type";




    @Override
    protected int getLayout() {
        return R.layout.ac_wxpay_entry;
    }

    @Override
    protected void initEventAndData() {
        setToolBar(toolbar, "支付详情");
        api = WXAPIFactory.createWXAPI(this, Constants.WX_APP_ID);
        api.handleIntent(getIntent(), this);
        initClick();
    }


    private void initClick(){
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ERR_OK.equals(payMsg)){
                    ToastUtil.show(WXPayEntryActivity.this,"支付成功");

                }else{
                    finish();
                }
            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK://成功
                    payMsg = ERR_OK;
                    btn_confirm.setVisibility(View.VISIBLE);





                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL://取消支付
                    payMsg = ERR_USER_CANCEL;
                    ToastUtil.show(getContext(),payMsg);
                    finish();
                    break;
                case BaseResp.ErrCode.ERR_COMM:// -1
                    payMsg = ERR_COMM;
                    ToastUtil.show(getContext(),payMsg);
                    finish();
                    break;
                default:
                    payMsg = ERR_DEFAULT;
                    ToastUtil.show(getContext(),payMsg);
                    finish();
                    break;
            }

        }
    }
}
